using System;
using System.Text.RegularExpressions;

namespace Quiki.ExpressionParsers
{
    public class LambdaExpressionParser : IWikiLineExpressionParser
    {
        public LambdaExpressionParser (string regexExpression, Func<string, WikiTextElement> elementFunc)
        {
            this.elementFunc = elementFunc;
            regex = new Regex (regexExpression, RegexOptions.Compiled);
        }

        public bool Process (string wikiText, WikiTextElement parentElement)
        {
            Match match = regex.Match (wikiText);
            if (false == match.Success)
                return false;

            WikiTextElement element = elementFunc(match.Groups["text"].Value);

            parentElement.ChildElements.Add(element);

            return true;
        }

        private readonly Regex regex;
        private readonly Func<string, WikiTextElement> elementFunc;
    }
}